﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using log4net;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.Admin.Def;
using CashFlow.Admin.Common;

namespace CashFlow.Admin.Service.Implement
{
    public class AdminService : Service<AdminEntity>, IAdminService
    {
        private static ILog _logger = LogManager.GetLogger(typeof(AdminService));

        public bool Login(string username, string pwd)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<AdminEntity>()
                                .Where(x => x.UserName == username && x.Pwd == pwd)
                                .First();
                if (entity != null)
                {
                    HttpContext.Current.Session[Consts.SESSIONKEY_LOGINUSERINFO] = entity;

                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public AdminEntity GetLoginUserInfo()
        {
            var entity = HttpContext.Current.Session[Consts.SESSIONKEY_LOGINUSERINFO] as AdminEntity;
            return entity;
        }

        public bool IsLogin()
        {
            var entity = GetLoginUserInfo();
            return entity != null;
        }

        public bool IsAdministrator()
        {
            var entity = GetLoginUserInfo();
            return MyCommonTool.IsAdministrator(entity);
        }

        public IList<AdminShopInfoView> QueryAll2()
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<AdminEntity>()
                                .LeftJoin<ShopInfoEntity>((a, s) => a.ShopManagerUserId == s.ShopManagerUserId)
                                .Select((a, s) => new AdminShopInfoView
                                {
                                    AdminInfo = a,
                                    ShopInfo = s
                                })
                                .ToList();
                return ls;
            }
        }

        public bool DeleteById(int adminId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Deleteable<AdminEntity>(x => x.Id == adminId
                                && x.UserName != MyConfig.AdminUserName)
                            .ExecuteCommand();
                return n > 0;
            }
        }

        public bool ResetPwd(string pwd
            , string newPwd
            , int adminId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<AdminEntity>()
                                .Where(x => x.Id == adminId && x.Pwd == pwd)
                                .First();
                if (entity != null && entity.Id > 0)
                {
                    db.Tracking(entity);
                    entity.Pwd = newPwd;
                    int n = db.Updateable(entity).ExecuteCommand();
                    return n > 0;
                }
                return false;
            }
        }

        public int AddAdminEntity(int shopManagerUserId
            , string username
            , string pwd
            , string name)
        {
            int result = (int)EAddAdminResult.Other;
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Queryable<ShopInfoEntity>()
                            .Where(x => x.ShopManagerUserId == shopManagerUserId)
                            .Count();
                if (n == 0)
                {
                    string text = string.Format("未查询到店铺信息, shopManagerUserId: {0}"
                                    , shopManagerUserId);
                    _logger.Error(text);
                }
                else
                {
                    int n2 = db.Queryable<AdminEntity>()
                                .Where(x => x.ShopManagerUserId == shopManagerUserId)
                                .Count();
                    if (n2 > 0)
                    {
                        string text = string.Format("店铺已被占用, shopManagerUserId: {0}"
                                        , shopManagerUserId);
                        _logger.Error(text);
                    }
                    else
                    {
                        int n3 = db.Queryable<AdminEntity>()
                                    .Where(x => x.UserName == username)
                                    .Count();
                        if (n3 > 0)
                        {
                            string text = string.Format("用户名已被占用, UserName: {0}"
                                            , username);
                            _logger.Error(text);
                            result = (int)EAddAdminResult.ExistUserName;
                        }
                        else
                        {
                            var entity = new AdminEntity
                            {
                                CreateTime = DateTime.Now,
                                Name = name,
                                Pwd = pwd,
                                ShopManagerUserId = shopManagerUserId,
                                UserName = username
                            };
                            int n4 = db.Insertable(entity).ExecuteCommand();
                            if (n4 > 0)
                            {
                                result = (int)EAddAdminResult.Success;
                            }
                        }
                    }
                }
            }
            return result;
        }
    }
}